#ifndef _BACKGROUNDPROGRAM_H_
#define _BACKGROUNDPROGRAM_H_
/***************************************************
 * mode name:BackgroundProgram
 * brief:用于管理所有任务，与mainWindow做区分
 * @author yxt
 * @created 2024/09/04 21:20:29
 * @imfor ~/bigSystem/closedLoop_regulation/include/BackgroundProgram.h
 * @modify:
 * 		
***************************************************/
#include "CollectBase.h"
#include "ConfigImport.h"
#include "sys_data_struct.h"
#include "GenEEGData.h"
#include "EmotionInterface.h"
#include "NetworkCore.h"
#include <QVariant>
#include <armadillo>
#include <vector>
#include <mutex>

Q_DECLARE_METATYPE(arma::cube)



class BackgroundProgram : public QObject
{
Q_OBJECT

public:
    // 采集对象
    CollectBase *eeg_dev;
    // 虚拟数据服务器，用于模拟在线数据流
    QProcess *simServer;
    ConfigImport *sys_config;
    // 文件目录
    QStringList fileNames;
    

public:
    BackgroundProgram(ConfigImport *sys_config, QObject *parent=nullptr);
    ~BackgroundProgram();

    int collectingDeviceInit();

    void collectingData();
    void stopCollecting();

    // int saveEEGData(const char* filePath, const GenEEGData& eegData, bool saveHead_flag = false);
    // GenEEGData loadEEGData(const char* filePath);


    int get_eegData(GenEEGData& target);
    arma::rowvec get_network() const;
    const QMap<int, QString>& getElectrodeNameMap();
    GenEEGData get_eegData();
    long long get_pointIndex();

    arma::cube cropValidData(std::vector<int> &label);



signals:
    void send_singleData(GenEEGData);
    void send_oneTrialData(GenEEGData);
    void send_network(const arma::rowvec &);

public slots:
    void slt_recEEGData(const QVariant& data);
    void slt_recNetwork(const arma::rowvec & network);

    void slt_setDataLabel(EmotionInterface::EmoMode label, int playMode, bool is_begin);
    void slt_setDataLabel_train(EmotionInterface::EmoMode label, bool is_begin);
    void slt_setDataLabel_test(EmotionInterface::EmoMode label, bool is_begin);

public: 
    // 缓存历史数据，用于波形复现和文件保存
    GenEEGData org_eegData;

private:
    // 单帧的脑电数据
    // arma::mat _eegData_single;
    GenEEGData _eegData_single;
    GenEEGData _eegData_oneTrial;

    
    GenEEGData filt_eegData;
    std::mutex _eegDataMutex;

    // 脑网络生成层
    NetworkCore* networkCore = nullptr;

    // 一个trial
    // arma::mat eegData_oneTrial;

    // org_eegData的索引，org_eegData清空，这个变量也要跟着清空
    long long pointIndex;

};
// Q_DECLARE_METATYPE(arma::cube)




#endif // !_BACKGROUNDPROGRAM_H_
